package com.way.filemanager.online;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.os.storage.StorageVolume;
import android.util.Log;

import com.way.filemanager.model.ParentDirectory;
import com.way.filemanager.preferences.FileManagerSettings;
import com.way.filemanager.preferences.Preferences;
import com.way.filemanager.util.CommonIdDef;
import com.way.filemanager.util.StorageHelper;

public class OnlineFilelistManager {

    private static final String TAG = "OnlineFilelistManager";
    Context mActivity;
    boolean mIsScaning = false;
    boolean mForceStop = false;
    StorageVolume[] volumes;
    long[] mTypeSize = new long[CommonIdDef.CATEGORY_ITEM_ID_MAX
            - CommonIdDef.CATEGORY_ITEM_ID_BASE];
    long mTotleMemory;
    long mAvailMemory;
    int mCurrentWorkType;
    public OnlineFilelistManager(Context m) {
        mActivity = m;
        volumes = StorageHelper.getStorageVolumes(mActivity);
        // FileListSQLiteOpenHelper mOpenHelper = new
        // FileListSQLiteOpenHelper(m);
    }

    public boolean isScaning() {
        return mIsScaning;

    }

    public void ForceStopScan() {
        mForceStop = true;

    }

    boolean isNeedIgnore(File f) {
        boolean ret = f.isDirectory();
        ret &= (f.getPath().indexOf("/.") == -1);
        return ret;
    }

    public int getFileType(String path) {
        int lastDot = path.lastIndexOf(".");
        if (lastDot <= 0)
            return 0;
        // Log.v("crash", "path:"+path);
        String ext = path.substring(lastDot + 1).toUpperCase(Locale.US);
        // Log.v("crash", "sFileTypeMap:"+sFileTypeMap);
        if (sFileTypeMap.containsKey(ext))
            return sFileTypeMap.get(ext);
        else
            return 0;
    }

    private static final HashMap<String, Integer> sFileTypeMap = new HashMap<String, Integer>();
    static {
        sFileTypeMap.put("MP3",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("MPGA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("APE",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("M4A",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("WAV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("AMR",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("AWB",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("DCF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("WMA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("OGG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("OGA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("AAC",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("MKA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("MID",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("MIDI",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("SMF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("IMY",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("RTX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("OTA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("MXMF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));

        sFileTypeMap.put("MP2",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("3GA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MOV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("QT",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MPEG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MPG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MP4",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("M4V",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("3GP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("3GPP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("3G2",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("3GPP2",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MKV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("WEBM",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("TS",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("MTS",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("M2TS",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("AVI",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("FLV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("F4V",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("PFV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("FLA",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("WMV",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));
        sFileTypeMap.put("ASF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_VIDEO));

        sFileTypeMap.put("JPG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("JPEG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("GIF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("PNG",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("BMP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("WBMP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("WEBP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));
        sFileTypeMap.put("MPO",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PICTURE));

        sFileTypeMap.put("TXT",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("HTM",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("HTML",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("PDF",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("DOC",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("XLS",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("PPT",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("FLAC",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_MUSIC));
        sFileTypeMap.put("DOCX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("DOTX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("XLSX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("XLTX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("PPTX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("POTX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));
        sFileTypeMap.put("PPSX",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_TEXT));

        sFileTypeMap.put("ZIP",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_ZIP));
        sFileTypeMap.put("APK",
                Integer.valueOf(CommonIdDef.CATEGORY_ITEM_ID_PACKAGE));

    }
    private List<OnlineFileInfo> lstPicFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstVidFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstMusicFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstApkFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstZipFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstTextFile = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstWorkingList = new ArrayList<OnlineFileInfo>();
    private List<OnlineFileInfo> lstAllFileList = new ArrayList<OnlineFileInfo>();
    /*
     * private void GetFiles(String Path,boolean IsIterative) { File[] files
     * =new File(Path).listFiles(); if((files == null)||mForceStop) return ;
     * List<OnlineFileInfo> filesName =new ArrayList<OnlineFileInfo>();
     * List<String> filesPath =new ArrayList<String>(); for (File f:files) { if
     * (f.isFile()) { String path =f.getName();
     * if(path.equalsIgnoreCase(".nomedia")) { filesName.clear();
     * filesPath.clear(); break; } int type = getFileType(path); //f.length();
     * if(type != 0) { FileSystemObject fileObj =
     * FileHelper.createFileSystemObject(f); OnlineFileInfo OnlineFileInfo= new
     * OnlineFileInfo(fileObj,type); filesName.add(OnlineFileInfo); } } else if
     * (isNeedIgnore(f)) { filesPath.add(f.getPath()); } } for(OnlineFileInfo
     * path:filesName) { //Log.v(TAG, "path:"+path); //int type =
     * getFileType(path.Filename); List<OnlineFileInfo> temp =
     * getFileList(path.mType); if(temp != null) { mTypeSize[path.mType -
     * CommonIdDef.CATEGORY_ITEM_ID_BASE] += path.GetFileSize(); temp.add(path);
     * } } if (IsIterative) { for(String path:filesPath) { GetFiles(path,
     * IsIterative); } } }
     */
    public int GetFileCount(int type) {
        return getFileList(type).size();
    }

    public int GetWorkingFileCount() {
        return lstWorkingList.size();
    }

    public long GetTotleSDSize() {
        return mTotleMemory;
    }

    public long GetTotleAvailSize() {
        return mAvailMemory;
    }

    public String GetFileName(int type, int pos) {
        List<OnlineFileInfo> lTemp = getFileList(type);
        if ((lTemp != null) && (lTemp.size() > pos)) {
            OnlineFileInfo t = lTemp.get(pos);
            return t.mfileName;
        }
        return null;
    }

    public long GetTypeSize(int type) {
        if ((type >= CommonIdDef.CATEGORY_ITEM_ID_BASE)
                && (type < CommonIdDef.CATEGORY_ITEM_ID_MAX))
            return mTypeSize[type - CommonIdDef.CATEGORY_ITEM_ID_BASE];
        else
            return 0;
    }

    public OnlineFileInfo GetOnlineFileInfo(int type, int pos) {
        List<OnlineFileInfo> lTemp = getFileList(type);
        if (lTemp != null)
            return lTemp.get(pos);
        else
            return null;
    }
    public OnlineFileInfo GetCurrentFileInfo(int pos)
    {
        if(lstWorkingList != null)
            return lstWorkingList.get(pos);
        else
            return null;
    }

    public List<OnlineFileInfo> getFileList(int type) {
        switch (type) {
        case CommonIdDef.CATEGORY_ITEM_ID_PICTURE:
            return lstPicFile;
        case CommonIdDef.CATEGORY_ITEM_ID_MUSIC:
            return lstMusicFile;
        case CommonIdDef.CATEGORY_ITEM_ID_VIDEO:
            return lstVidFile;
        case CommonIdDef.CATEGORY_ITEM_ID_PACKAGE:
            return lstApkFile;
        case CommonIdDef.CATEGORY_ITEM_ID_TEXT:
            return lstTextFile;
        case CommonIdDef.CATEGORY_ITEM_ID_ZIP:
            return lstZipFile;
        case CommonIdDef.CATEGORY_ITEM_ID_Search:
            return getSearchList();
        case CommonIdDef.CATEGORY_ITEM_ID_Select:
            return getSelectList();
        case CommonIdDef.CATEGORY_ITEM_ID_ALL:
            return lstAllFileList;
        default:
            break;
        }
        return null;
    }

    public void startSearch(int type) {
        mCurrentWorkType = CommonIdDef.CATEGORY_ITEM_ID_Search;
        lstWorkingList.clear();
        lstWorkingList.addAll(getFileList(type));
    }

    public List<OnlineFileInfo> SearchString(int type, String key) {
        List<OnlineFileInfo> temp = getFileList(type);
        lstWorkingList.clear();
        for (OnlineFileInfo m : temp) {
            if (m.mfileName.contains(key))
                lstWorkingList.add(m);
        }
        return lstWorkingList;
    }

    public List<OnlineFileInfo> getSearchList() {
        return lstWorkingList;
    }

    public void startSelect(int type) {
        mCurrentWorkType = CommonIdDef.CATEGORY_ITEM_ID_Search;
        lSelectedCount = 0;
        lstWorkingList.clear();
        lstWorkingList.addAll(getFileList(type));
        for (OnlineFileInfo m : lstWorkingList) {
            m.SetFileSelect(false);
        }
    }

    public List<OnlineFileInfo> getSelectList() {
        return lstWorkingList;
    }

    int lSelectedCount = 0;

    public void SelectedAdd() {
        lSelectedCount++;
        if (lSelectedCount > lstWorkingList.size())
            lSelectedCount = lstWorkingList.size();
    }

    public void SelectedDec() {
        lSelectedCount--;
        if (lSelectedCount < 0)
            lSelectedCount = 0;
    }

    public void  SelectedAll()
    {
        for(OnlineFileInfo m:lstWorkingList)
        {
            m.mSelected = true;
        }
        lSelectedCount= lstWorkingList.size();
    }
    public void  UnSelectedAll()
    {
        for(OnlineFileInfo m:lstWorkingList)
        {
            m.mSelected = false;
        }
        lSelectedCount = 0;
    }

    public void startNormal(int type)
    {
        lSelectedCount = 0;
        lstWorkingList.clear();
        mCurrentWorkType = type;

        Preferences.getSharedPreferences();
        FileManagerSettings showHiddenPref = FileManagerSettings.SETTINGS_SHOW_HIDDEN;
        if(Preferences.getSharedPreferences().getBoolean(showHiddenPref.getId(),
                 ((Boolean) showHiddenPref.getDefaultValue()).booleanValue()))
        {
            lstWorkingList.addAll(getFileList(type));
        }
        else
        {
            for(OnlineFileInfo m:getFileList(type))
            {
                if(!m.mfileName.startsWith("."))
                    lstWorkingList.add(m);
            }
        }
        //sortByFullPath(lstWorkingList);
    }


    public int GetSelectedCount() {
        return lSelectedCount;
    }

    public OnlineFileInfo[] GetSelectCurrentFileInfo() {
        OnlineFileInfo[] lTemp = new OnlineFileInfo[lSelectedCount];
        int i = 0;
        for (OnlineFileInfo m : lstWorkingList) {
            if (m.mSelected) {
                lTemp[i++] = m;
            }
        }
        return lTemp;
    }

    public void addWorkingList(List<OnlineFileInfo> list ){
        if(lstWorkingList!=null){
            lstWorkingList.addAll(list);
        }
       // Log.e(TAG,"addWorkingList lstWorkingList size is "+lstWorkingList.size());
        for(OnlineFileInfo path:list)
        {
            //Log.e(TAG, "addWorkingList path is "+path);
            //int type = getFileType(path.Filename);
            List<OnlineFileInfo> temp = getFileList(path.mType);
            if(temp != null)
            {
                //Log.e(TAG, "addWorkingList temp size  is "+temp.size());
                //below is used calculate one type file total size
                //mTypeSize[path.mType - CommonIdDef.CATEGORY_ITEM_ID_BASE] += path.mFileSize;
                temp.add(path);
            }
        }
        //sortByFullPath(lstWorkingList);

    }
    public void clearAll(){
        if(lstWorkingList!=null){
            lstWorkingList.clear();
        }
        if(lstPicFile!=null){
            lstPicFile.clear();
        }
        if(lstVidFile!=null){
            lstVidFile.clear();
        }
        if(lstMusicFile!=null){
            lstMusicFile.clear();
        }
        if(lstApkFile!=null){
            lstApkFile.clear();
        }
        if(lstZipFile!=null){
            lstZipFile.clear();
        }
        if(lstTextFile!=null){
            lstTextFile.clear();
        }
        if(lstAllFileList!=null){
            lstAllFileList.clear();
        }

    }

    public void sortByFullPath(List<OnlineFileInfo> lstFileList) {
        Collections.sort(lstFileList, new Comparator<OnlineFileInfo>() {
            @Override
            public int compare(OnlineFileInfo lhs, OnlineFileInfo rhs) {

                boolean isLhsParentDirectory = lhs.mIsFolder;
                boolean isRhsParentDirectory = rhs.mIsFolder;
                if (isLhsParentDirectory || isRhsParentDirectory) {
                    if (isLhsParentDirectory && isRhsParentDirectory) {
                        return lhs.mFullPath.compareToIgnoreCase(rhs.mFullPath);
                    }
                    return (isLhsParentDirectory) ? -1 : 1;
                }

                return lhs.mFullPath.compareToIgnoreCase(rhs.mFullPath);

            }

        });
    }
}
